/**
 * @author Hemant
 * Report.java
 * This class finds text file path for a report
 */


/*
*Released on 28th June 2008.
*Any violations can be reported at paritosh@wikiocean.net
*What is treated as violations can be found at www.wikiocean.net/ppl1-voilations*
******************************************************************************
* The contents of this file are subject to POOL Public License 1.0 and later.
* POOL Public License is based on Affero GPL v3 and additional conditions.
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the Affero GPL v3 License at http://www.gnu.org/licenses/agpl.html
* You may obtain a copy of the POOL Public License 1.0 or later at www.wikiocean.net/license/ppl.html
* Software distributed under POOL Public License 1.0 is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Initial Developer of the Original Code is Paritosh Pungaliya (C) 2008. All Rights Reserved.
******************************************************************************
* Objective of the additional terms (license)
* 1) Is to extend the software freedom to freedom to work.
* 2) To ensure that knowledge is free from monopoly of agencies.
* 3) To avoid a situation where big corporate or investor can buy out free software groups and companies and then start milking the communities built around it.
* (this trend can be seen how the freedom is curtailed in companies that get Venture Capital funding.)
******************************************************************************
*/

package  guireportgenerator;

/*import java.io.*;

import dbmanager.Column;
import dbmanager.DBManager;
import dbmanager.GetIdName;

import utility.Input;

public class Report implements WebReportInterface, Input 
{
	String mid,param,reportname;
	RandomAccessFile raf;
	StringBuffer buffer;
	GetIdName get;
	
	*//**
	 * Overridden method of WebReportInterface interface. 
	 * @param db DBManager object.
	 *//*
	public void setDatabase(DBManager db) 
	{
		this.db = db;
	}
	*//**
	 * Overridden method of WebReportInterface interface.
	 * @param get GetIdName object. 
	 *//*
	public void setGetIdName(GetIdName get)
	{
		this.get = get;
	}
	
	*//**
	 * Sets repport name.
	 * @param reportname
	 *//*
	public Report(String reportname) 
	{
		this.reportname = reportname;
	}
	*//**
	 * Overridden method of WebReportInterface interface.
	 * @param Parameter for report.
	 *//*
	public void setData(String param) 
	{
		this.param = param;
	}
	
	

		

}
*/


import guiproperty.HTMLInterface;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Hashtable;
import java.util.LinkedList;
import java.util.RandomAccess;
import java.util.Vector;

import query.view.Query;


import database.RecordSearch;
import dbmanager.*;

import utility.Input;

public class Report implements HTMLInterface
{
	String tt,id="",param;
	String ReportUpper = "reportupper";
	String BillReport = "billreport";
	String masterreport = null;
	DBManager database;
	Vector<Object> conditionvector =new Vector<Object>();
	// Defined by Hemant
	String mid,reportname;
	RandomAccessFile raf;
	StringBuffer buffer;	
	GetIdName get;
	
	public Report(String tt, String id) 
	{
		this.tt = tt;
		this.id = id;
	}
	
	public Report(String masterreport)
	{
		this.masterreport = masterreport;	
		this.reportname = masterreport;
	}
		
	public String[] getReportData()
	{	
		try
		{ 	
			Query reportview = new  Query();
			//reportview.setDbmanager();
			reportview.setReportname(reportname);
			reportview.SetConditionmap(conditionvector);
			reportview.initializeData();
			reportview.getResult();
			return reportview.getbuffer();
		}
		catch (Exception e) 
		{
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * Overridden method of WebReportInterface interface.
	 * @return Report path.
	 */
	public String getData()
	{
		
       	Hashtable<Object, Object> hash = new Hashtable<Object, Object>();
       		
   		hash.put("mid", get.getId(masterreport));
   		hash.put("pid", get.getId("storage"));
   		hash.put("td", "null");
   		
   		RecordSearch rec = new RecordSearch();
   		rec.setConditionMap(hash);
   		ResultSet rs = rec.getResultSet();
   		String storage = null;
   		       
   		StringBuffer sbr = new StringBuffer();
   		
   		
   		try {      			
   			
			while(rs.next())
				storage = rs.getString("PV");
			
			hash = new Hashtable<Object, Object>();
			hash.put("mid", storage);
       		hash.put("pid", get.getId("path"));
       		hash.put("td", "null");
       		
       		rec = new RecordSearch();
       		rec.setConditionMap(hash);
       		rs = rec.getResultSet();
       		
       		String path = null;
       		while(rs.next())
       		{
				path = rs.getString("PV");					
				if(path.equalsIgnoreCase("0"))
					path = rs.getString("VT");
       		}
       		
       		path = Input.DISPLAYTEXT_FILES+path+Input.FILRFORMAT;
       		RandomAccessFile raf = null;
			try {
				raf = new RandomAccessFile(path, "r");
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
       		
       		String line = null;
       		
       		try {
				while((line=raf.readLine())!=null)
				{
					sbr.append(line);
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
              
       return sbr.toString();
	}
	
	public Hashtable<Object, Object> getHash()
	{
		Hashtable<Object, Object> ResultMap = new Hashtable<Object, Object>();
		try
		{ 	
			Query reportview = new  Query();
			//reportview.setDbmanager(database);
			reportview.setReportname(reportname);
			reportview.SetConditionmap(conditionvector);
			reportview.initializeData();
			reportview.getResult();
			String buffer[]= reportview.getbuffer();
			String heading[] = buffer[0].split("\t");
			for(int i=1;i<buffer.length;i++)
			{
				String splitbuffer[] = buffer[i].split("\t");
				for(int j=0;j<splitbuffer.length;j++)
					ResultMap.put(heading[j].toLowerCase(), splitbuffer[j]);
			}
		}
		catch (Exception e) 
		{
			e.printStackTrace();
		}
		return ResultMap;
	}
	public String getHTMLCode() {
		// TODO Auto-generated method stub
		return null;
	}

	public void setDatabase(DBManager db) 
	{
	 this.database = db;	
	}

	public void setGetIdName(GetIdName get) {
		// TODO Auto-generated method stub
		
	}

	public void setLine(String line) {
		// TODO Auto-generated method stub
		
	}

	public void setObjPropertyList(
			Hashtable<Object, LinkedList<Object>> ObjectPropertyList) {
		// TODO Auto-generated method stub
		
	}

	public void setObjectName(String ObjectName) {
		// TODO Auto-generated method stub
		
	}

	public void setParam(String param) {
		// TODO Auto-generated method stub
		
	}

	public void setReport(String report) {
		// TODO Auto-generated method stub
		
	}

	public void setTag(String tag) {
		// TODO Auto-generated method stub
		
	}

	public void setText(String text) {
		// TODO Auto-generated method stub
		
	}

	public void setParamHash(Hashtable<Object, Object> hash)
	{
		Hashtable<Object, Object> newhash=new Hashtable<Object, Object>();
		newhash.putAll(hash);
		this.conditionvector.add(newhash);
	}

	public void setNoSelectObject(String noselect) 
	{
		
	}

	public void setSelectObject(String select) {
		// TODO Auto-generated method stub
		
	}

	public void isEnd(boolean end) {
		// TODO Auto-generated method stub
		
	}
	
	public void setData(String param)
	{
		this.param = param;
	}
	
	public static void main(String[] args) 
	{		
		GetIdName get = new GetIdName();
		Report report = new Report("categoryreport");
		//report.setDatabase();
		report.setGetIdName(get);
		Hashtable<Object, Object> hash = new Hashtable<Object, Object>();
		//hash.put("mastername", "nirma det soap");
		hash.put("type", "master");
		report.setParamHash(hash);

		//report.setData("1545");
		report.getReportData();
				
	}

	public void setHeaderLine(String header) {
		// TODO Auto-generated method stub
		
	}
	
}
